package com.ruoyi.web.controller.system;

import cn.hutool.core.io.FileUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.RecordFile;
import com.ruoyi.system.domain.RecordFileBook;
import com.ruoyi.system.service.IRecordFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * 文件管理Controller
 *
 * @author admin
 * @date 2022-03-02
 */
@RestController
@RequestMapping("/system/file")
public class RecordFileController extends BaseController {
    @Autowired
    private IRecordFileService recordFileService;

    /**
     * 查询文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecordFile recordFile) {
        startPage();
        List<RecordFile> list = recordFileService.selectRecordFileList(recordFile);
        return getDataTable(list);
    }

    /**
     * 查询文件管理台账
     */
    @PreAuthorize("@ss.hasPermi('system:file:listBook')")
    @GetMapping("/listBook")
    public TableDataInfo list(RecordFileBook recordFile) {
        startPage();
        List<RecordFileBook> list = recordFileService.selectRecordFileListBook(recordFile);
        return getDataTable(list);
    }

    /**
     * 导出文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "文件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecordFile recordFile) {
        List<RecordFile> list = recordFileService.selectRecordFileList(recordFile);
        ExcelUtil<RecordFile> util = new ExcelUtil<RecordFile>(RecordFile.class);
        util.exportExcel(response, list, "文件管理数据");
    }

    /**
     * 导出文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "案卷包导出", businessType = BusinessType.EXPORT)
    @PostMapping("/anJuanBaoExport/{archivesId}")
    public void anJuanBaoExport(@PathVariable("archivesId") String archivesId, HttpServletRequest request, HttpServletResponse response) {
        //生成文件并反回
        File resfile = recordFileService.selectRecordFileExport(archivesId);

        try {
            String downloadName = "案卷.zip";
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, downloadName);
            // FileUtils.writeBytes(resfile.getAbsolutePath(), response.getOutputStream());
            FileUtil.writeToStream(resfile, response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            resfile.delete();
        }
    }

    /**
     * 导出文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:exportBook')")
    @Log(title = "文件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/exportBook")
    public void export(HttpServletResponse response, RecordFileBook recordFile) {
        List<RecordFileBook> list = recordFileService.selectRecordFileListBook(recordFile);
        ExcelUtil<RecordFileBook> util = new ExcelUtil<RecordFileBook>(RecordFileBook.class);
        util.exportExcel(response, list, "文件管理数据");
    }

    /**
     * 获取文件管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId) {
        return AjaxResult.success(recordFileService.selectRecordFileByFileId(fileId));
    }

    /**
     * 新增文件管理
     */
    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordFile recordFile) {
        return toAjax(recordFileService.insertRecordFile(recordFile));
    }

    /**
     * 修改文件管理
     */
    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordFile recordFile) {
        return toAjax(recordFileService.updateRecordFile(recordFile));
    }

    /**
     * 删除文件管理
     */
    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds) {
        return toAjax(recordFileService.deleteRecordFileByFileIds(fileIds));
    }

    /**
     * 文件导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<RecordFile> util = new ExcelUtil<RecordFile>(RecordFile.class);
        util.importTemplateExcel(response, "文件数据");
    }

    /**
     * 文件导入
     */
    @Log(title = "文件导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:file:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<RecordFile> util = new ExcelUtil<RecordFile>(RecordFile.class);
        List<RecordFile> fileList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = recordFileService.importFile(fileList, operName);
        return AjaxResult.success(message);
    }
}
